import CryptoJS from '../node_modules/crypto-js/crypto-js.js';
import Base64 from 'base-64';
var keyvi = '1ea53d260ecf11e7b56e00163e046a26';

/* des加密 */
const des_encrypt = (message) => {
	var keyHex = CryptoJS.enc.Utf8.parse(keyvi);
	var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
		mode: CryptoJS.mode.ECB,
		padding: CryptoJS.pad.Pkcs7
	});
	return encrypted.toString();
}

function base_encrypt(message) {
	var en = Base64.encode(message);
	return en;
}

//des解密 DES-EDE3-CBC
function des_decrypt(message) {
	var key = CryptoJS.MD5(keyvi).toString();
	var iv = CryptoJS.MD5(keyvi).toString();
	var crypto_key = CryptoJS.enc.Utf8.parse(key);
	var crypto_iv = CryptoJS.enc.Utf8.parse(iv.substr(0, 8));
	var decrypt_str = CryptoJS.TripleDES.decrypt(message, crypto_key, {
		iv: crypto_iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	return decrypt_str.toString(CryptoJS.enc.Utf8);
}

function desAndBase(message) {
	var key = CryptoJS.MD5(keyvi).toString();
	var iv = CryptoJS.MD5(keyvi).toString();
	var crypto_key = CryptoJS.enc.Utf8.parse(key);
	var crypto_iv = CryptoJS.enc.Utf8.parse(iv.substr(0, 8));

	var encode_str = CryptoJS.TripleDES.encrypt(message, crypto_key, {
		iv: crypto_iv,
		mode: CryptoJS.mode.CBC,
		padding: CryptoJS.pad.Pkcs7
	});
	var str = Base64.encode(encode_str.toString());
	
	return str;
}
module.exports = {
	des_encrypt: des_encrypt,
	base_encrypt: base_encrypt,
	desAndBase: desAndBase
}
